package com.zxx.ps;

import com.rabbitmq.client.*;
import com.zxx.utils.ConnectionUtil;

import java.io.IOException;

import static com.zxx.ps.Producer.FANOUT_EXCHAGE;
import static com.zxx.ps.Producer.FANOUT_QUEUE_2;

public class Consumer2 {

    public static void main(String[] args) throws Exception {

        Connection connection = ConnectionUtil.getConnection();

        Channel channel = connection.createChannel();

        channel.exchangeDeclare(FANOUT_EXCHAGE,BuiltinExchangeType.FANOUT);

        channel.queueDeclare(FANOUT_QUEUE_2, true, false, false, null);

        channel.queueBind(FANOUT_QUEUE_2,FANOUT_EXCHAGE,"");

        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                    System.out.println("路由："+envelope.getRoutingKey());
                    System.out.println("交换机："+envelope.getExchange());
                    System.out.println("消息id为："+envelope.getDeliveryTag());
                    System.out.println("接收到的消息为："+new String(body, "utf-8"));

            }
        };

        channel.basicConsume(FANOUT_QUEUE_2,true,defaultConsumer);
    }
}
